package com.weng.sso.server.api;


import com.weng.framework.core.model.ResponseData;
import com.weng.sso.core.config.SsoConfig;
import com.weng.sso.core.constants.LoginTypeEnum;
import com.weng.sso.core.model.SsoUser;
import com.weng.sso.server.helper.SsoWebLoginHelper;
import com.weng.sso.server.service.AuthService;
import com.weng.sso.server.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * sso server (for web)
 *
 */
@Controller
public class WebController {

    @Autowired
    private AuthService authService;

    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) {

        SsoUser ssoUser = SsoWebLoginHelper.loginCheck(request, response);

        if (ssoUser == null) {
            return "redirect:/login";
        } else {
            model.addAttribute("ssoUser", ssoUser);
            return "index";
        }
    }

    @RequestMapping(SsoConfig.SSO_LOGIN)
    public String login(Model model, HttpServletRequest request, HttpServletResponse response) {

        SsoUser ssoUser = SsoWebLoginHelper.loginCheck(request, response);

        if (ssoUser != null) {
            String redirectUrl = request.getParameter(SsoConfig.REDIRECT_URL);
            if (redirectUrl!=null && redirectUrl.trim().length()>0) {

                String sessionId = SsoWebLoginHelper.getSessionIdByCookie(request);
                String redirectUrlFinal = redirectUrl + "?" + SsoConfig.SSO_SESSIONID + "=" + sessionId;;

                return "redirect:" + redirectUrlFinal;
            } else {
                return "redirect:/";
            }
        }

        model.addAttribute("errorMsg", request.getParameter("errorMsg"));
        model.addAttribute(SsoConfig.REDIRECT_URL, request.getParameter(SsoConfig.REDIRECT_URL));
        return "login";
    }


    @RequestMapping("/do-login")
    public String doLogin(HttpServletRequest request,
                        HttpServletResponse response, RedirectAttributes redirectAttributes,
                        String userName, String password, String ifRemember) {

        boolean ifRem = (ifRemember!=null&&"on".equals(ifRemember))?true:false;

        ResponseData result = authService.login(userName, password, LoginTypeEnum.LOGIN_COOKIE.getCode());

        if(!result.isSuccess()){
            redirectAttributes.addAttribute("errorMsg", result.getMessage());
            redirectAttributes.addAttribute(SsoConfig.REDIRECT_URL, request.getParameter(SsoConfig.REDIRECT_URL));
            return "redirect:/login";
        }
        SsoUser ssoUser = (SsoUser)result.getData();

        CookieUtil.set(response, SsoConfig.SSO_SESSIONID, ssoUser.getToken(), ifRem);

        String redirectUrl = request.getParameter(SsoConfig.REDIRECT_URL);
        if (redirectUrl!=null && redirectUrl.trim().length()>0) {
            String redirectUrlFinal = redirectUrl + "?" + SsoConfig.SSO_SESSIONID + "=" + ssoUser.getToken();
            return "redirect:" + redirectUrlFinal;
        } else {
            return "redirect:/";
        }

    }

    @RequestMapping(SsoConfig.SSO_LOGOUT)
    public String logout(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {

        SsoWebLoginHelper.logout(request, response);

        redirectAttributes.addAttribute(SsoConfig.REDIRECT_URL, request.getParameter(SsoConfig.REDIRECT_URL));
        return "redirect:/login";
    }


}